package com.farm.quartz.controller;

import com.farm.common.core.domain.AjaxResult;
import com.farm.quartz.Dao.haohaoAnimalsDao;
import com.farm.quartz.Dao.haohaoMessageDao;
import com.farm.quartz.Dao.haohaoProductDao;
import com.farm.quartz.Dao.haohaoTypeDao;
import com.farm.quartz.model.haohaoAnimals;
import com.farm.quartz.model.haohaoMessage;
import com.farm.quartz.model.haohaoProduct;
import com.farm.quartz.model.haohaoType;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
public class demo {
    @Resource
    haohaoMessageDao haohaoMessageDao;
    @Resource
    haohaoTypeDao haohaoTypeDao;
    @Resource
    haohaoAnimalsDao haohaoAnimalsDao;
    @Resource
    haohaoProductDao haohaoProductDao;
    @GetMapping("product/message/selectList")
    public AjaxResult list(int pageNum,int pageSize){
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        List<haohaoMessage> list = haohaoMessageDao.findAllBy(pageable);
        return AjaxResult.success(String.valueOf(haohaoMessageDao.countAllBy()),list);
    }

    @PostMapping("product/message/selectListByName")
    public AjaxResult ListByName(@RequestBody Map<String , String>query){
        List<haohaoMessage> list = haohaoMessageDao.findByName(query.get("name"));
        return AjaxResult.success(String.valueOf(list.size()),list);
    }

    @PostMapping("product/message/editMessage")
    public AjaxResult editMessage(@RequestBody haohaoMessage haohaoMessage){
        try{
            haohaoMessageDao.save(haohaoMessage);
            return AjaxResult.success(true);
        }catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @PostMapping("product/message/delMessage")
    public AjaxResult delMessage(@RequestBody Map<String ,Integer>data){
        try{
            haohaoMessageDao.deleteById(data.get("id"));
            return AjaxResult.success(true);
        }
        catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @PostMapping("product/message/addProduct")
    public AjaxResult addMessage(@RequestBody haohaoMessage haohaoMessage){
        return editMessage(haohaoMessage);
    }

    @PostMapping("product/type/addType")
    public AjaxResult addProduct(@RequestBody haohaoType haohaoType){
        return editType(haohaoType);
    }

    @GetMapping("product/type/selectTypeList")
    public AjaxResult typeList(int pageNum,int pageSize){
        Pageable pageable = PageRequest.of(pageNum -1,pageSize);
        List<haohaoType> list = haohaoTypeDao.findAllBy(pageable);
        return AjaxResult.success(String.valueOf(haohaoTypeDao.countAllBy()),list);
    }

    @PostMapping("product/type/selectListByTypeName")
    public AjaxResult ListByTypeName(@RequestBody Map<String , String>query){
        List<haohaoType> list = haohaoTypeDao.findByName(query.get("name"));
        return AjaxResult.success(String.valueOf(list.size()),list);
    }

    @PostMapping("product/type/editType")
    public AjaxResult editType(@RequestBody haohaoType haohaoType){
        try{
            haohaoTypeDao.save(haohaoType);
            return AjaxResult.success(true);
        }catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @PostMapping("product/type/delType")
    public AjaxResult delType(@RequestBody Map<String ,Integer>data){
        try{
            haohaoTypeDao.deleteById(data.get("id"));
            return AjaxResult.success(true);
        }
        catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @GetMapping("animals/selectAnimalsList")
    public AjaxResult selectAnimalsList(int pageNum,int pageSize){
        Pageable pageable = PageRequest.of(pageNum -1,pageSize);
        List<haohaoAnimals> list = haohaoAnimalsDao.findAllBy(pageable);
        return AjaxResult.success(String.valueOf(haohaoAnimalsDao.countAllBy()),list);
    }

    @PostMapping("animals/selectAnimalsListByName")
    public AjaxResult AnimalsListByName(@RequestBody Map<String , String>query){
        List<haohaoAnimals> list = haohaoAnimalsDao.findByName(query.get("name"));
        return AjaxResult.success(String.valueOf(list.size()),list);
    }

    @PostMapping("animals/editAnimals")
    public AjaxResult editAnimals(@RequestBody haohaoAnimals haohaoAnimals){
        try{
            haohaoAnimalsDao.save(haohaoAnimals);
            return AjaxResult.success(true);
        }catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @PostMapping("animals/addAnimals")
    public AjaxResult addAnimals(@RequestBody haohaoAnimals haohaoAnimals){
        return editAnimals(haohaoAnimals);
    }

    @PostMapping("animals/delAnimals")
    public AjaxResult delAnimals(@RequestBody Map<String ,Integer>data){
        try{
            haohaoAnimalsDao.deleteById(data.get("id"));
            return AjaxResult.success(true);
        }
        catch (Exception e){
            return AjaxResult.success(false);
        }
    }

    @GetMapping("product/factory/selectFactoryList")
    public AjaxResult selectFactoryList(int pageNum,int pageSize){
        Pageable pageable = PageRequest.of(pageNum -1,pageSize);
        List<haohaoProduct> list = haohaoProductDao.findAllBy(pageable);
        return AjaxResult.success(String.valueOf(haohaoProductDao.countAllBy()),list);
    }

    @PostMapping("product/factory/selectFactoryListByName")
    public AjaxResult selectFactoryListByName(@RequestBody Map<String , String>query){
        List<haohaoProduct> list = haohaoProductDao.findByName(query.get("name"));
        return AjaxResult.success(String.valueOf(list.size()),list);
    }

    @PostMapping("product/factory/editFactoryList")
    public AjaxResult editFactoryList(@RequestBody haohaoProduct haohaoProduct){
        try{
            haohaoProductDao.save(haohaoProduct);
            return AjaxResult.success(true);
        }catch (Exception e){
            System.out.println(e.toString());
            return AjaxResult.success(false);
        }
    }

    @PostMapping("product/factory/addFactoryList")
    public AjaxResult addFactoryList(@RequestBody haohaoProduct haohaoProduct){
        return editFactoryList(haohaoProduct);
    }

    @PostMapping("product/factory/delFactoryList")
    public AjaxResult delFactoryList(@RequestBody Map<String ,Integer>data){
        try{
            haohaoProductDao.deleteById(data.get("id"));
            return AjaxResult.success(true);
        }
        catch (Exception e){
            return AjaxResult.success(false);
        }
    }
}
